#include "mainwindow.h"
#include <QAction>
#include <QtGui>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //setAttribute(Qt::WA_DeleteOnClose);
    setWindowTitle("Tangram Creator 0.1");
    setMinimumSize(420, 594);

    tangramWidget = new TangramWidget(this);
    setCentralWidget(tangramWidget);

    createActions();
    createMenus();
}

void MainWindow::createActions()
{
    printAction = new QAction(tr("&Print..."), this);
    printAction->setShortcut(QKeySequence::Print);
    printAction->setStatusTip(tr("Create a new spreadsheet file"));
    connect(printAction, SIGNAL(triggered()), this, SLOT(print()));

    exitAction = new QAction(tr("&Exit"), this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("Exit the application"));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(printAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);
}

void MainWindow::print()
{
    QPrinter printer(QPrinter::HighResolution);
    printer.setPaperSize(QPrinter::A4);
    printer.setFullPage(true);
    printer.setResolution(300);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName("tangram.pdf");
    QPrintPreviewDialog preview(&printer, this);
    preview.setMinimumSize(420, 594);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
    preview.exec();
}

void MainWindow::print(QPrinter* printer)
{
    QPainter painter(printer);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.scale(6, 6);
    tangramWidget->view->viewport()->render(&painter);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    event->accept();
}

MainWindow::~MainWindow()
{

}
